package com.cy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
public class ScaProviderApplication {

    private static final Logger log=
            LoggerFactory.getLogger(ScaProviderApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ScaProviderApplication.class,args);
    }

    //当配置中心的内容发生变化,重新发布时,用于刷新类中的配置信息
    @RefreshScope
    @RestController
    public class ProviderController{

        @Value("${server.tomcat.threads.max:200}")
        private Integer maxThread;

        @RequestMapping("/provider/doGetMaxThread")
        public String doGetMaxThread(){
            return "server.threads.max is  "+maxThread;
        }

        @Value("${page.pageSize:10}")
        private Integer pagesize;

        @RequestMapping("/provider/doGetPageSize")
        public String doGetPageSize(){
            return "pagesize is  "+pagesize;
        }


        @Value("${server.port}")
        private String server;
        /**读取配置中心的日志级别*/
        @Value("${logging.level.com.cy:error}")//":"后面为默认值
        private String logLevel;

        @GetMapping("/provider/logLevel")
        public String doGetLogLevel(){
            //System.out.println("log.level="+logLevel);
            //日志优先级 trace<debug<info<warn<<error
            log.trace("==log.trace==");//跟踪
            log.debug("==log.debug==");//调试
            log.info("==log.info==");//常规信息
            log.warn("==log.warn==");//警告
            log.error("==log.error==");//错误信息
            return "log level is "+logLevel;
        }

        @GetMapping("/provider/echo/{msg}")
        public String doEcho(@PathVariable String msg) throws InterruptedException {//Echo为回显的含义
            //Thread.sleep(500000);//模拟耗时操作
            //通过如下语句默认服务调用过程中的异常
            if(msg==null||msg.length()<3)
                throw new IllegalArgumentException("参数不合法");
            return server+" say: hello "+msg;
        }
    }
}
